/**
 * 
 */

/**
 * 初始化开心一刻
 */
function initJokes(ulId){ 
	$.ajax({
		type:'GET',
		url:'http://apis.baidu.com/showapi_open_bus/showapi_joke/joke_text?page='+parseInt(Math.random()*10%9),
		dataType:'json',
		beforeSend : function(request) {
			request.setRequestHeader("apikey","3e0130d4cd21e1976732a43f3b4259f7");
		},
		success:function(result){
			var jokelist=result.showapi_res_body.contentlist;
			var j=parseInt(Math.random()*10%10);
			var joke=jokelist[j];
			$('#joke_title').text(joke.title);
			$('#joke_text').text(joke.text);
			/*for(var i=0;i<jokelist.length;i++){ 
				var joke=jokelist[i];
				console.log(joke); 
			}*/
		}
	})
}

/* 刷新blog列表  */
function refreshBlogListUL(path,page){
	$("#blog_list_ul li").remove();
	 
	$.ajax({  
		type : "POST", 
		url : path+"/blog_list.action",
		data:{
			title:$("#common_right_search_input").val(),
			page:page
		},
		dataType : 'json',  
		success : function(result) {
			
			initPage(path,page,result.pageBean.totalPages);
			var data=result.data;
			for(var i=0;i<data.length;i++){   
				var datetime=data[i].createTime; 
//				var datetime=new Date(data[i].createTime.year,data[i].createTime.month,data[i].createTime.date,data[i].createTime.hours,data[i].createTime.minutes,data[i].createTime.seconds).format("( MM/dd HH:mm )")
				
				var $e=$('<li> <h4 style="font-size:18px;"> <img style="width:15px;height:15px;" src="../images/index/boke.png" ></img> &nbsp;  <a href="blog_getById.action?id='+data[i].uuid+'">'+data[i].title+'&nbsp;&nbsp; <small>  ( '+datetime+' )</small></a></h4><div class="blog_list_shortcontent">'+data[i].subtitle+'</div></li>');
				$('#blog_list_ul').append($e);
			}
		} 
	}); 
}

/**
 * 初始化分页插件
 * @param curPage
 * @param totalPages 
 */
function initPage(path,curPage,totalPages){ 
	
	
	if(totalPages==0){
		totalPages=1;  
	}
	
	var options = {
			currentPage : curPage,
			totalPages : totalPages,
			numberOfPages : 5,  
			size:"normal",
			alignment:"right",  
			/*pageUrl : function(type, page, current) { 
				return path + "/blog_blogList.action?rows=10&page=" + page;
			}*/
			onPageClicked: function(e,originalEvent,type,page){
				refreshBlogListUL(path,page);
            }
		}
	$('#page1').bootstrapPaginator(options);
	$("#page1").removeClass("pagination-right");
	$("#page1").addClass("pagination-centered");  
	$("#page1").addClass("toCenter");  
}

/**
 * 初始化右部 最新博客
 * @param path
 */
function initLastBlogUL(path){
	$("#common_right_lastblogs_ul li").remove();
	$.ajax({
		type : "GET", 
		url : path+"/blog_list.action?page=1&rows=5",
		dataType : 'json', 
		success : function(result) { 
			var data=result.data;
			for(var i=0;i<data.length;i++){   
				var datetime=data[i].createTime;
				
				// 获取格式化日期
//				var datetime=new Date(data[i].createTime.year,data[i].createTime.month,data[i].createTime.date,data[i].createTime.hours,data[i].createTime.minutes,data[i].createTime.seconds).format("( MM/dd HH:mm )")
				//<li><a href="#">常见的java代码优化方法<small> -- lizi</small></a></li>
				var $e=$('<li><a href="blog_getById.action?id='+data[i].uuid+'">'+data[i].title+'<small>    '+data[i].userNickName+'</small></a></li>');
				$('#common_right_lastblogs_ul').append($e);
			}
		}
	});
}
